package com.draw.cli.gui;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.HashMap;

import javax.swing.JFrame;
import javax.swing.JPanel;

import com.draw.cli.gui.panel.Refreshable;
import com.draw.cli.gui.panel.config.ConfigPanel;
import com.draw.cli.gui.panel.draw.DrawPanel;
import com.draw.cli.gui.panel.home.HomePanel;
import com.draw.cli.gui.panel.login.LoginPanel;
import com.draw.cli.gui.panel.newUser.NewUserPanel;
import com.draw.cli.gui.panel.play.PlayPanel;

public class Window extends JFrame implements WindowListener, Refreshable {
	private static final long serialVersionUID = 3814780871328494597L;

	private static final String HOME = "home";
	private static final String NEWUSER = "new_user";
	private static final String LOGIN = "login";
	private static final String CONFIG = "config";
	private static final String DRAW = "draw";
	private static final String PLAY = "play";

	private static Window instance = null;

	public static Window getInstance() {
		if (instance == null) {
			Loading.start();
			instance = new Window();
			Loading.stop();
		}
		return instance;
	}

	private JPanel center;
	private HashMap<String,Refreshable> panels;
	private String current;

	public Window() {
		super();
		this.addWindowListener(this);
		this.setExtendedState(MAXIMIZED_BOTH);
		this.setTitle("DrawSomeEnsi");
		// this.setSize(800, 600);

		this.setJMenuBar(MenuBar.getInstance());

		this.add(StateBar.getInstance(), BorderLayout.SOUTH);

		this.center = new JPanel(new CardLayout());

		this.panels = new HashMap<String,Refreshable>();
		this.center.add(HomePanel.getInstance(), HOME);
		this.panels.put(HOME,HomePanel.getInstance());
		this.center.add(NewUserPanel.getInstance(), NEWUSER);
		this.panels.put(NEWUSER,NewUserPanel.getInstance());
		this.center.add(LoginPanel.getInstance(), LOGIN);
		this.panels.put(LOGIN,LoginPanel.getInstance());
		this.center.add(ConfigPanel.getInstance(), CONFIG);
		this.panels.put(CONFIG,ConfigPanel.getInstance());
		this.center.add(DrawPanel.getInstance(), DRAW);
		this.panels.put(DRAW,DrawPanel.getInstance());
		this.center.add(PlayPanel.getInstance(), PLAY);
		this.panels.put(PLAY,PlayPanel.getInstance());

		this.add(this.center, BorderLayout.CENTER);

		this.setLocationRelativeTo(null);
		this.setHome();
	}

	private void setPanel(String name) {
		CardLayout la = (CardLayout) this.center.getLayout();
		la.show(this.center, name);
		this.current = name;
		this.refresh();
	}

	public void setHome() {
		this.setPanel(HOME);
	}

	public void setNewUser() {
		this.setPanel(NEWUSER);
	}

	public void setLogin() {
		this.setPanel(LOGIN);
	}

	public void setConfig() {
		this.setPanel(CONFIG);
	}

	public void setDraw() {
		this.setPanel(DRAW);
	}

	public void setPlay() {
		this.setPanel(PLAY);
	}

	public void exit() {
		System.exit(0);
	}

	public void windowActivated(WindowEvent arg0) {
	}

	public void windowClosed(WindowEvent arg0) {
	}

	public void windowClosing(WindowEvent arg0) {
		this.exit();
	}

	public void windowDeactivated(WindowEvent arg0) {
	}

	public void windowDeiconified(WindowEvent arg0) {
	}

	public void windowIconified(WindowEvent arg0) {
	}

	public void windowOpened(WindowEvent arg0) {
	}

	@Override
	public void refresh() {
		this.panels.get(this.current).refresh();
		StateBar.getInstance().refresh();
	}

	public static void main(String arg[]) {
		Window win = Window.getInstance();
		win.setVisible(true);
	}
}
